//
//  HACTecView.m
//  Pods
//
//  Created by Hotacool on 2017/8/31.
//
//

#import "HACTecView.h"
#import "HACBasicComponents.h"
#import "HACTimeLineEntry.h"
#import "HACKLineEntry.h"
#import "HACIndexEntry.h"

#import "HACTecProvider.h"

@interface HACTecView ()
/** 指数数据Provider */
@property (nonatomic, strong) id<HACTecProvider> tecProvider;
@end

@implementation HACTecView {
}

- (instancetype)initWithViewType:(HACTecViewType)viewType andIndexType:(HACTecViewIndexType)indexType {
    if (self = [super init]) {
        [self setUp];
        [self setViewType:viewType andIndexType:indexType];
    }
    return self;
}

- (void)setUp {
    [super setUp];
    // 初始化页面属性
    self.backgroundColor = [UIColor clearColor];
    self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.xAxisCount = 2;
    self.yAxisCount = 5;
    self.singleWidth = 1;
    self.itemsSpace = 1;
    self.visualCount = 241;
    
    _rectRenderer = [[HACRectRenderer alloc] initWithDataProvider:self];
    _lineRenderer = [[HACLineRenderer alloc] initWithDataProvider:self];
    
    self.leftAxis.labelCount = 2;
}

- (void)setData:(HACChartData *)data {
    if (_tecProvider) {
        [_tecProvider setData:data];
    }
    [super setData:data];
}

- (void)clean {
    if (_tecProvider) {
        [_tecProvider setData:nil];
    }
    [super clean];
}

- (void)addIncrementData:(HACChartData *)data {
    [super addIncrementData:data];
    if (_tecProvider) {
        [_tecProvider addIncrementData:data];
    }
}

- (void)setViewType:(HACTecViewType)viewType andIndexType:(HACTecViewIndexType)indexType {
    _viewType = viewType;
    _indexType = indexType;
    switch (viewType) {
        case HACTecViewTypeKLine: {
            switch (indexType) {
                case HACTecViewIndexTypeVol: {
                    self.yAxisCount = 5;
                    self.xAxisCount = 2;
                    self.itemsSpace = 1;
                    self.visualCount = 50;
                    
                    [self setTecProvider:[NSClassFromString(@"HACKLineVolProvider") new]];
                    break;
                }
                case HACTecViewIndexTypeRsi: {
                    
                    break;
                }
                default:
                    break;
            }
            break;
        }
        case HACTecViewTypeTimeLine: {
            //目前只有成交量
            self.xAxisCount = 2;
            self.yAxisCount = 5;
            self.itemsSpace = 1;
            self.visualCount = 241;
           
            [self setTecProvider:[NSClassFromString(@"HACTLineVolProvider") new]];
            break;
        }
    }
}

- (NSInteger)actualCount {
    if (_tecProvider) {
        return _tecProvider.actualCount;
    }
    return 0;
}

- (NSInteger)startIndex {
    return _tecProvider?_tecProvider.startIndex:0;
}

- (void)setStartIndex:(NSInteger)startIndex {
    _tecProvider.startIndex = startIndex;
}

- (NSInteger)endIndex {
    return _tecProvider?_tecProvider.endIndex:0;
}

- (void)setEndIndex:(NSInteger)endIndex {
    _tecProvider.endIndex = endIndex;
}

- (CGFloat)maxValue {
    return _tecProvider?_tecProvider.maxValue:0;
}

- (CGFloat)minValue {
    return _tecProvider?_tecProvider.minValue:0;
}

- (void)setTecProvider:(id<HACTecProvider>)tecProvider {
    _tecProvider = tecProvider;
    _tecProvider.rootProvider = self;
    _rectRenderer.provider = tecProvider;
    _lineRenderer.provider = tecProvider;
    
    if (self.data) {
        [tecProvider setData:self.data];
    }
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.rectRenderer drawInRect:rect context:context];
    [self.lineRenderer drawInRect:rect context:context];
}

#pragma mark -- method
- (DrawingValueType)calculatorValueType:(HACTimeLineEntry *)currentData lastData:(HACTimeLineEntry *)lastData {
    if (currentData.newprice < lastData.newprice)
        return DrawingDownValue;
    else
        return DrawingUpValue;
}

// 计算最大，最小值
- (void)calculateLimitValue {
    [self.tecProvider calculateLimitValue];
}
@end
